<template>
	<view class="container" >
		<view class="status_bar">
			<view style="margin-left: 4%;" @tap="goBack()"><u-icon name="arrow-leftward" size='24' /></u-icon></view>
			<view style="margin-left: -50upx; font-weight: bold;">资料设置</view>
			<view></view>

		</view>
		
		<view class="shop-box">
			<view class="con-shop" @tap="goremrk">
				<view style="display: flex;">
					<view style="margin-top: 10upx;">设置备注</view>
				</view>
				<uni-icons type="arrowright" size="18" color="#eaeaea"></uni-icons>
			</view>
			
			<view class="con-shop">
				<view style="display: flex;">
					<view style="margin-top: 10upx;">置顶聊天</view>
				</view>
				<view>
					<u-switch v-model="value1" @change="change1" size=20 activeColor="#1ea7dd"></u-switch>
				</view>
			</view>
			<view class="con-shop">
				<view style="display: flex;">
					<view style="margin-top: 10upx;">消息免打扰</view>
				</view>
				<view>
					<u-switch v-model="value2" @change="change2" size=20 activeColor="#1ea7dd"></u-switch>
				</view>
			</view>
			
		</view>
		
		<view class="shop-box">
			<view class="con-shop">
				<view style="display: flex;">
					<view style="margin-top: 10upx;">加入黑名单</view>
				</view>
				<view>
					<u-switch v-model="value3" @change="change3" size=20 activeColor="#1ea7dd"></u-switch>
				</view>
			</view>
		</view>
		<view class="shop-box" @tap="recordShow=true">
			<view class="con-shop">
				<view style="display: flex;">
					<view style="margin-top: 10upx;">清空聊天记录</view>
				</view>
				<uni-icons type="arrowright" size="18" color="#eaeaea"></uni-icons>
			</view>
		</view>
		
		<view class="shop-box" @tap="deleteShow=true">
			<view class="con-shop">
				<view style="display: flex;">
					<view style="margin-top: 10upx; color: #d84e65;">删除联系人</view>
				</view>
				<uni-icons type="arrowright" size="18" color="#eaeaea"></uni-icons>
			</view>
		</view>
		
		<!-- 删除联系人弹框-->
		<u-popup :show="deleteShow" @close="deleteClose" mode="center" :round="20">
			<view class="clear-record-box">
				<view class="clear-record-box-title">提示</view>
				<view class="clear-record-box-title1">是否删除此联系人？</view>
				<view class="clear-record-box-btn">
					<view class="clear-record-box-btn-left" @tap="deleteClose()">取消</view>
					<view class="clear-record-box-btn-right" @tap="deleteConFirm()">确认</view>
				</view>
			</view>
		</u-popup>
		<!-- 清除聊天记录弹框-->
		<u-popup :show="recordShow" @close="recordsClose" mode="center" :round="20">
			<view class="clear-record-box">
				<view class="clear-record-box-title">提示</view>
				<view class="clear-record-box-title1">确定要清除所有聊天记录？</view>
				<view class="clear-record-box-btn">
					<view class="clear-record-box-btn-left" @tap="recordsClose()">取消</view>
					<view class="clear-record-box-btn-right" @tap="recordsConFirm()">确认</view>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	let disturbhashMap;
	let tophashMap;
	export default {
		data() {
			return {
				userId: uni.getStorageSync('userId') || '',
				startX: 0, // 触摸开始的X坐标
				endX: 0, // 触摸结束的X坐标
				threshold: 50, // 设置滑动多少距离后触发返回
				value2: false,
				value1: false,
				value3: false,
				user: {},
				
				recordShow: false, //聊天记录

				info: '',
				deleteShow: false,
				destructTitle: '请选择',
				destructName: {
					name: '关闭'
				},
				
			}
		},
		onLoad(option) {
			this.info = JSON.parse(option.groupId)
			console.log(this.info)
			disturbhashMap = uni.getStorageSync('disturbhashMap' + uni.getStorageSync("user_uid"));
			tophashMap = uni.getStorageSync('tophashMap' + uni.getStorageSync("user_uid"));
			if (disturbhashMap) {
				disturbhashMap = new Map(JSON.parse(disturbhashMap));

				this.value2 = disturbhashMap.get(this.info.user_uid) ?? false;
			} else {
				disturbhashMap = new Map();
			}
			if (tophashMap) {
				tophashMap = new Map(JSON.parse(tophashMap));

				this.value1 = tophashMap.get(this.info.user_uid) ?? false;
			} else {
				tophashMap = new Map();
			}
		},
		onShow() {
			this.getblacklist();

		},
		methods: {
			goremrk(item) {
				this.toPage('/pages/contacts/edittRemark?groupId=' + JSON.stringify(this.info));
			},
			goBack() {
				uni.navigateBack({
					delta: 1
				});
			},
			
			
			
			change(e) {
				console.log('change', e);
			},
			change1(e) {
				console.log('change', e);
				tophashMap.set(this.info.user_uid, e);
				uni.setStorageSync('tophashMap' + uni.getStorageSync("user_uid"), JSON.stringify(Array.from(
					tophashMap)))

				data = `is_top = ${e}`
				where = `relatedId = '${this.info.user_uid}'`
				this.DB.updateTableData("chat" + uni.getStorageSync("user_uid"), data, where)
					.then(res => {

						this.$forceUpdate();
					})
					.catch(error => {
						console.log("修改失败", error);
					});

			},
			change2(e) {
				console.log('change', e);
				disturbhashMap.set(this.info.user_uid, e);
				uni.setStorageSync('disturbhashMap' + uni.getStorageSync("user_uid"), JSON.stringify(Array.from(
					disturbhashMap)))

				data = `is_mute = ${e}`
				where = `relatedId = '${this.info.user_uid}'`
				this.DB.updateTableData("chat" + uni.getStorageSync("user_uid"), data, where)
					.then(res => {
						this.$forceUpdate();
					})
					.catch(error => {
						console.log("修改失败", error);
					});

			},
			getblacklist() {
				uni.request({
					url: this.$api.baseUrl + '/api/common/black_list/fetch',
					header: {
						'content-type': 'application/json'
					},
					data: {
						user_id: uni.getStorageSync('userId'),
						page: 1,
						page_size: 30,
					},
					method: 'POST',
					success: res => {
						console.log(res);
						// uni.hideLoading()
						if (res.data.code == 0) {
							let list = res.data.data;
							let index = list.findIndex(item => (item.black_id == this.info.user_uid && item
								.user_id == uni.getStorageSync('userId')) || (item.black_id == uni
								.getStorageSync('userId') && item.user_id == this.info.user_uid));
							if (index >= 0) {
								this.hmd_id = list[index].id
								this.value3 = true;
							} else {
								this.hmd_id = '';
								this.value3 = false;
							}

							console.log("判断是否黑名单", this.value3)

						}

					},
					fail: res => {
						console.log(res);
					}
				})
			},
			addblack() {

				uni.request({
					url: this.$api.baseUrl + '/api/common/black_list/add',
					header: {
						'content-type': 'application/json'
					},
					data: {
						// black_id: Number(this.info.user_uid),
						// user_id: Number(uni.getStorageSync('userId'))

						user_id: Number(uni.getStorageSync('userId')),
						black_id: Number(this.info.user_uid),
					},
					method: 'POST',
					success: res => {
						console.log(uni.getStorageSync('userId'));
						console.log(this.info);
						console.log(res);
						uni.hideLoading()
						if (res.data.code == 0) {

							this.getblacklist();
						}


						console.log(this.alllist);
					},
					fail: res => {
						console.log(res);
					}

				})
			},
			deleteblack() {
				uni.request({
					// url: this.$api.baseUrl + '/api/common/black_list/delete',
					url: this.$api.baseUrl + '/api/user/remove/blacklist',
					header: {
						'content-type': 'application/json'
					},
					data: {
						// id: this.hmd_id,
						user_id: Number(uni.getStorageSync('userId')),
						black_id: Number(this.info.user_uid),
					},
					method: 'POST',
					success: res => {
						console.log(res);

						// uni.hideLoading()
						if (res.data.code == 0) {
							// this.value_hmd = true;

							this.getblacklist();
						}

					},
					fail: res => {
						console.log(res);
					}
				})
			},
			change3(e) {
				console.log('change', e);
				if (e) {
					this.addblack();
				} else {
					this.deleteblack();
				}
			},
			//消息自毁弹框事件
			destructClose() {
				this.destructShow = false
			},
			destructSelectClick(index) {
				this.destructName = index
			},
			//删除联系人事件
			deleteClose() {
				this.deleteShow = false
				// console.log('close');
			},
			deleteConFirm() {


				const newDataObj = {
					local_uid: uni.getStorageSync('user_uid'),
					friend_uid: this.info.user_uid,
				};
				const newDataJsonStr = JSON.stringify(newDataObj);

				const requestData = {
					"processorId": '1008',
					"jobDispatchId": '5',
					"actionId": '7',
					"newData": newDataJsonStr
				};
				const requestDataJsonStr = JSON.stringify(requestData);
				uni.request({
					url: 'http://chat.zwsmcn.com:8090/rest_post',
					header: {
						'content-type': 'application/json'
					},
					data: requestDataJsonStr,
					method: 'POST',
					success: res => {
						this.DB.deleteTableData("friend" + uni.getStorageSync("user_uid"), 'user_uid', this
								.info.user_uid)
							.then(res => {
								console.log("删除表数据成功", res);
								uni.navigateBack({
									delta: 2
								});

							})
							.catch(error => {
								console.log("删除失败", error);
							});

					},
					fail: res => {
						console.log(res);
					}
				});
				this.deleteShow = false
			},
			//清除聊天记录事件
			recordsOpen() {
				console.log('open');
				this.recordShow = false
			},
			handleRecord() {
				this.recordShow = true
			},
			recordsClose() {
				this.recordShow = false
			},
			recordsConFirm() {
				let newDataObj = {};

				if (this.info.g_id == '' || this.info.g_id == '0') {
					newDataObj = {
						luid: uni.getStorageSync('user_uid'),
						ruid: this.info.relatedId,
					};
				} else {
					newDataObj = {
						luid: uni.getStorageSync('user_uid'),
						gid: this.info.g_id,
					};
				}

				const newDataJsonStr = JSON.stringify(newDataObj);

				const requestData = {
					"processorId": '1008',
					"jobDispatchId": '5',
					"actionId": '8',
					"newData": newDataJsonStr
				};
				console.log(requestData);
				const requestDataJsonStr = JSON.stringify(requestData);
				uni.request({
					url: 'http://chat.zwsmcn.com:8090/rest_post',
					header: {
						'content-type': 'application/json'
					},
					data: requestData, // requestDataJsonStr,
					method: 'POST',
					success: res => {
						console.log(res);
						let data = `del_flag = 1`;
						let where = `(relatedId = '${this.info.relatedId}')`
						// 修改表数据 DB.updateTableData(表名, 要修改的列名=修改后列值, 修改条件的列名, 修改条件的列值)
						this.DB.updateTableData("chat" + uni.getStorageSync("user_uid"), data, where)
							.then(res => {
								console.log(res);
								uni.navigateBack({
									delta: 2
								});
							})
							.catch(error => {
								console.log("修改失败", error);
							});
						// uni.navigateBack({
						// 	delta: 2
						// });
					},
					fail: res => {
						console.log(res);
					}
				});
				this.recordShow = false
			},
		}
	}
</script>

<style lang="scss" scoped>
	page {
		overflow: hidden;
	}

	.container {
		width: 100%;
		height: 100%;
		background: #f4f7fc;
	}

	.status_bar {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-top: 90upx;
		background: #fff;
	}

	.status_bar,
	.head {
		background-color: #fff;
		height: 160upx;
		padding-bottom: 60upx;

		.face {
			width: 160upx;
			height: 160upx;
			border-radius: 10upx;
		}

		.info-name {
			font-size: 40upx;
			font-weight: bold;
		}

		.info-num {
			padding: 2upx 0upx;
			border-radius: 30upx;
			margin-top: 21upx;
			color: #5d5d5d;
		}

		.copybtns {
			color: #4457a4;
			margin-top: 21upx;
		}

		.info-box {
			margin-left: 30upx;
		}
	}

	.shop-box {
		background-color: #fff;
		padding: 0upx 40upx;
		color: #2e2e2e;
		font-weight: 300;
		//margin-top: 14upx;
		margin-bottom: 14upx;

		.con-shop {
			height: 100upx;
			display: flex;
			justify-content: space-between;
			align-items: center;
		}
	}

	.clear-record-box {
		border-radius: 30upx;
		padding: 30upx;

		.clear-record-box-title {
			padding: 10upx 20upx;
			font-size: 40upx;
		}

		.clear-record-box-title1 {
			padding: 20upx 0 30upx 20upx;
			font-size: 30upx;
			font-weight: 300;
		}

		.clear-record-box-btn {
			display: flex;
			// padding: 30upx 0 20upx 0;
			margin-left: 150upx;
		}

		.clear-record-box-btn-left {
			padding: 10upx 30upx;
		}

		.clear-record-box-btn-right {
			background: #000;
			color: #fff;
			padding: 10upx 40upx;
			margin-left: 30upx;
		}
	}
</style>